<?php

//echo br(3); ###########################################################################################

	$TEMP->set_file(array('handle' => DIR_VIEW_STUDENTS . 'myEnrollment.html'));
	
	$STUDID		= $logUserID;
	
	$getStudentInformation 		= $STUD->getStudentInformation($STUDID);
	$getFreshmenSemesterLinker 	= $DSC->getFreshmenSemesterLinker($getStudentInformation['course_idFK']);
	$stud_idPK 			= $getStudentInformation['stud_idPK'];
	$stud_no 			= $getStudentInformation['stud_no'];
	$first_name 		= $getStudentInformation['first_name'];
	$middle_name 		= $getStudentInformation['middle_name'];
	$last_name 			= $getStudentInformation['last_name'];
	$course_idFK 		= $getStudentInformation['course_idFK'];
	$course_desc 		= $getStudentInformation['course_desc'];
	$dept_desc 			= $getStudentInformation['dept_desc'];
	$course_desc 		= $getStudentInformation['course_desc'];
	
	$semAlreadyTaken 	= $STUD->getSemAlreadyTeken($STUDID, $course_idFK);
	
	$semTaken = '';
	$semMultiple = 0;
	if ( !empty($semAlreadyTaken) ) {
		foreach($semAlreadyTaken as $semAlreadyTakenValue) {
			$semTaken .= $semAlreadyTakenValue['stud_sem_active'] . ',';
			$semTakenLast = $semAlreadyTakenValue['stud_sem_active'];
			$semMultiple++;
		}
		$semTakenID = rtrim($semTaken, ',');
	}

	$semesterLinker	= $DSC->getSemesterLinkerKeyArrangement($course_idFK, $semTakenID, $semMultiple);

	$semNextSY = ( $semesterLinker['sem_year'] == 4 )? $DSC->getSemesterLinkerFouthYear(4, $semesterLinker['sem_linker_idPK']) : '';
	$semNextTaken = $semesterLinker['sem_linker_idPK'];
	
	//$countSubjectTaken	= $DSC->getCountSubjectTaken($STUDID, $course_idFK, $semTakenLast);
	$previousSemester	= $DSC->getSemesterLinkerPreviousSemester($course_idFK, $semNextTaken);
	$prevCountSem = 0;
	if ( !empty($previousSemester) ) {
		foreach($previousSemester as $ps) {
			$countSubjectTaken	= $DSC->getCountSubjectTaken($STUDID, $course_idFK, $ps['sem_linker_idFK']);
			if ( $countSubjectTaken['count'] != $ps['count'] ) {
				$prevCountSem++;
			}
		}
	}
	
	$semesterLabel	= $DSC->getSemesterLabel($semNextTaken);
	
	if ( isset($_POST['subButton']) ) {
		
		$if_error		= 0;
		
		$active_subj_taken	= _POST('active_subj_taken');
		$listsubjtaken	 	= _POST('list_subj_taken');
		
		if ( empty($listsubjtaken) ) { $if_error++; }

		if ( $if_error == 0 ) {
			$expListsubjtaken = explode(', ', $listsubjtaken);
			$studSubjEnrollID = $STUD->getInsertStudSubjEnrollIDViaID($stud_idPK, $course_idFK, $activeYear, $expListsubjtaken, $active_subj_taken, $semesterLinker['sem_linker_idPK']);
			redirect('./');
		}
		
	}
	else {
		$recordSaved = '0';
	}
	
	$activeSemesterStud = $STUD->getSemesterEnrollmentActive($STUDID);

	$semSubjList = $STUD->getStudentSubjectTakenListSeperateBySem($STUDID, $course_idFK, $activeSemesterStud['sem_linker_idFK']);

	if ( !empty($semSubjList) ) {
		$activeSubj  	= '';
		$subjFailed  	= '';
		$TEMP->set_block('handle',  'subjects', 'subjectsRow');
		foreach($semSubjList as $semSubjListField => $studentSubjectList) {
			$semDesc = $DSC->getSemesterDescViaLinker($semSubjListField);
			$subjListHTML = '<table width="0" border="0" cellspacing="0" cellpadding="0" class="table-list-semester-subject">
								<tr>
									<td height="20" colspan="3" class="subject-taken-value-line" align="center"><span class="font-blue"><strong>' . $semDesc['sem_code'] . '</span></strong></td>
								</tr>
								<tr>
									<td height="5" colspan="3"></td>
								</tr>';
			if (!empty($studentSubjectList)) {
				$count = 1;
				foreach($studentSubjectList as $studentSubjectListVal) {
					
					if (!empty($studentSubjectListVal['grade'])) {
						if ($studentSubjectListVal['grade'] == 'INC' || $studentSubjectListVal['grade'] == 'DRP') {
							$myGrades = '<span class="font-red"><strong>'.$studentSubjectListVal['grade'].'</strong></span>';
							$activeSubj .= $studentSubjectListVal['subj_idPK'] . '-';
							$subjFailed .= $studentSubjectListVal['subj_idPK'] . '-';
						}
						else {
							
							$gradeIfPercent = $globalCon->getGradeIfPercent($studentSubjectListVal['grade']);
							if ( $studentSubjectListVal['grade'] == '4'     ||
								 $studentSubjectListVal['grade'] == '4.0'   ||
								 $studentSubjectListVal['grade'] == '5'  	||
								 $studentSubjectListVal['grade'] == '5.0'   ||
								 ($studentSubjectListVal['grade'] <  '75'	&&	 $studentSubjectListVal['grade'] >  '5'	)	) {
									$gradeValue = '<span class="font-red"><strong>'.$studentSubjectListVal['grade'].'</strong></span>';
									$activeSubj .= $studentSubjectListVal['subj_idPK'] . '-';
									$subjFailed .= $studentSubjectListVal['subj_idPK'] . '-';
							}
							else {
								$gradeValue = $studentSubjectListVal['grade'];
							}
							
							$gradeIfPercent = $globalCon->getGradeIfPercent($studentSubjectListVal['grade']);
							if ( $gradeIfPercent === true ) {
								$myGrades 	  = $gradeValue;
								$gradeConvert = '--';
							}
							else {
								$myGrades = $gradeValue.'%';
								$gradeConvert = $STUD->gradeConvertion($studentSubjectListVal['grade']);
							}
						}
					}
					else {
						$myGrades = '';
					}
					$subjListHTML .= '<tr valign="middle">
										  <td width="12" class="subject-taken-value">&nbsp;<span class="numbering-previous-grade">' . $count . '.</span></td>
										  <td width="78" class="subject-taken-value">&nbsp;' . $studentSubjectListVal['subj_code'] . '</td>
										  <td width="60" class="subject-taken-value align="center"">
										  	<div class="subject-grades">
												<div class="subject-grades-value">
										  			&nbsp;' . $myGrades . '
												</div>
												<div class="subject-grades-value font-green">
										  			&nbsp;' . $gradeConvert['equivalence'] . '
												</div>
											</div>
										  </td>
									  </tr>';
					$count++;
				}
			}
			$subjListHTML .= '</table><div class="subj-list-spacer"></div>';

			$TEMP->set_var(array('subj_list' => $subjListHTML));
			$TEMP->parse('subjectsRow', 'subjects', true);
		}
		
	}
	else {
		$TEMP->set_var(array('subj_list' => ''));
	}

	$TEMP->set_var(array('stud_id'			=> $stud_idPK,
						 'stud_no'			=> $stud_no,
						 'first_name'		=> $first_name,
						 'middle_name'		=> $middle_name,
						 'last_name'		=> $last_name,
						 'course_id'		=> $course_idFK,
						 'course_desc'		=> $course_desc,
						 'dept_desc'		=> $dept_desc,
						 'sem_taken_active'	=> $activeSemesterStud['sem_linker_idFK'],
						 'active_subj'		=> rtrim($activeSubj,'-'),
						 'sem_linker_id'	=> $semTakenID,
						 'sem_taken_id'		=> $semTakenID,
						 'sem_next_taken'	=> $semNextTaken,
						 'sem_desc'			=> $semesterLabel['sem_desc'],
						 'sem_code'			=> $semesterLabel['sem_code'],
						 'total_subj_unit'	=> $totalSubjUnit,
						 'dean_sub_menu'	=> $pageLib->deanSubMenu(DIR_IMAGES),
						 'redirect'			=> $REDIRECT,
						 'hash'				=> $HASH,
						 'record_saved'		=> $recordSaved,
						 'active_subj_taken'=> $active_subj_taken,
						 'fourth_next_sem'	=> $semNextSY,
						 'subj_failed'		=> $subjFailed,
						 'inc_taken_subj'	=> $prevCountSem,
						 'is_regular'		=> $initSemActive['is_regular']
						));
						
	$TEMP->set_var(array('stud_sub_menu'	=> $studentDesignName,
						 'stud_active_sem'	=> $studActiveSemester,
						 'announcement'		=> $announcement,
						));
?>